

#include <iostream>
using namespace std;
#include <string>

class Sln1
{
public:
    string reverseWords(string s)
    {
        size_t l = 0, r = 0;
        size_t sz = s.size();

        while (r < sz)
        {
            while (r < sz && s[r] != ' ')
            {
                ++r;
            }
            reverse(s.begin() + l, s.begin() + r);

            l = ++r;
        }

        return s;
    }
};

class Sln2
{
public:
    void _multiply(const string& str, const char c, string& ret, int subs)
    {
        int sz = str.size();

        if ('0' == c)
        {
            while (sz--)
            {
                ret.push_back('0');
            }
        }
        else
        {
            int flag = 1, j = 0;
            for (int i = sz - 1; i >= 0; --i)
            {
                int a = str[i] - '0', b = c - '0';
                int ab = a * b * flag;

                if (0 == ab / 10)
                    flag = 1;
                else
                    flag = ab / 10;

                ret[j++] = ab % 10 + '0';
            }

            if (1 != flag)
                ret[j++] = flag + '0';
        }

        if (ret.size() < str.size() + subs)
        {
            while (subs--)
            {
                ret.push_back('0');
            }
        }

        reverse(ret.begin(), ret.end());
    }

    string _sub(const string& s1, const string& s2)
    {
        if (1 == s1.size() && '0' == s1[0])
            return s2;
        else if (1 == s2.size() && '0' == s2[0])
            return s1;
        else
        {
            std::reverse(s1.begin(), s1.end());
            std::reverse(s2.begin(), s2.end());

            string ret;
            size_t sz1 = s1.size(), sz2 = s2.size();
            size_t i = 0, j = 0, k = 0;
            int flag = 0;
            while (i < sz1 || j < sz2 || flag)
            {
                int a = i >= sz1 ? 0 : s1[i++] - '0';
                int b = j >= sz2 ? 0 : s2[j++] - '0';
                int ab = a + b + flag;

                ret[k++] = ab % 10 + '0';
                flag = ab / 10;
            }
            std::reverse(ret.begin(), ret.end());

            return ret;
        }
    }

    void _submul(string& ret, const string* ps, const int sz)
    {
        ret = ps[0];
        for (int i = 1; i < sz; ++i)
        {
            ret = _sub(ret, ps[i]);
        }
    }

    string multiply(string num1, string num2)
    {
        if ((1 == num1.size() && '0' == num1[0])
            || (1 == num2.size() && '0' == num2[0]))
            return "0";

        string l_str = num1, s_str = num2;
        if (num1.size() < num2.size())
        {
            l_str = num2;
            s_str = num1;
        }

        string* ps = new string[s_str.size()];

        int s_sz = s_str.size();
        for (int i = s_sz - 1; i >= 0; --i)
        {
            _multiply(l_str, s_str[i], ps[s_sz - 1 - i], s_sz - 1 - i);
        }

        string ret;
        _submul(ret, ps, s_sz);

        return ret;
    }
};

int main()
{
    /*int i = 1;
    for (; i < 10; ++i)
    {
        cout << 999 * i << endl;
    }*/
    string num1 = "2", num2 = "3";

    // err
    string s = Sln2().multiply(num1, num2);
    cout << s << endl;

	return 0;
}